package com.fulin.leetcode;

/**
 * @Description 寻找2个正序数组的中位数
 * @Author fulin16
 * @Date 2020/10/14 15:42
 **/
public class Num4 {

    public static void main(String[] args) {
        Num4 n = new Num4();
        int[] nums1 = {1,2}, nums2 = {3,4,5};
        System.out.println(n.findMedianSortedArrays(nums1,nums2));
    }

    //归并之后再查找
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int len1 = nums1.length;
        int len2 = nums2.length;
        int len = len1 + len2;
        int[] top = new int[len1 + len2];

        int index1 = 0;
        int index2 = 0;
        for(int i = 0 ; i < len; i++){
            if(index1 < len1 && index2 < len2){
                top[i] = Math.min(nums1[index1],nums2[index2]);
                if(nums1[index1] < nums2[index2]){
                    index1 ++;
                }else{
                    index2 ++;
                }
            }else{
                if(index1 == len1){
                    top[i] = nums2[index2];
                    index2 ++;
                }else if(index2 == len2){
                    top[i] = nums1[index1];
                    index1 ++;
                }
            }

        }

        int index = len & 1;
        if( index == 0) {
            //偶数
            int mid = len / 2;
            return (top[mid] + top[mid - 1]) / 2.0;
        }else{
            //奇数
            int mid = len / 2;
            return top[mid];
        }
    }

    //不经过归并,直接使用2个指针进行
    public double findMedianSortedArrays2(int[] nums1, int[] nums2) {
        int len1 = nums1.length;
        int len2 = nums2.length;
        int top = len1 + len2;

        int eval = top & 1;

        int index1 = 0;
        int index2 = 0;
        if(eval == 0){
            //偶数
           int mid =  top / 2;
           int low = mid - 1;

           int i = 0;
           while(i < low){

           }

        }else{
            //奇数
            int mid = top / 2;
        }

        return 0;
    }
}
